package com.difsonapps.contacttranslator.dao;

import org.hibernate.SessionFactory;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.difsonapps.contacttranslator.dto.EventDTO;
import com.difsonapps.contacttranslator.dto.UserDTO;

@Repository
@Transactional
public class EventDAO {
	@Autowired
	protected SessionFactory sessionFactory;

	@Transactional
	public void insert(EventDTO eventDTO) {
		sessionFactory.getCurrentSession().save(eventDTO);
	}

	@Transactional
	public void update(EventDTO eventDTO) {
		sessionFactory.getCurrentSession().update(eventDTO);
	}

	@Transactional
	public EventDTO get(int id) {
		return (EventDTO) sessionFactory.getCurrentSession().get(
				EventDTO.class, id);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public List<EventDTO> getAll() {
		return (List<EventDTO>) sessionFactory.getCurrentSession()
				.createQuery("Select t from EventDTO t").list();
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public List<EventDTO> getByLastId(int lastId, int limit) {
		
		return  (List<EventDTO>) sessionFactory.getCurrentSession()
				.createQuery("Select t from EventDTO t where t.id>:lastid")
				.setMaxResults(limit)
				.setParameter("lastid", lastId).list();
		
	}
	
	@SuppressWarnings("unchecked")
	@Transactional
	public List<EventDTO> getLimit(int limit) {
		return (List<EventDTO>) sessionFactory.getCurrentSession()
				.createQuery("Select t from EventDTO t").setMaxResults(limit).list();
		
	}

	@Transactional
	public void delete(EventDTO eventDTO) {
		sessionFactory.getCurrentSession().delete(eventDTO);
	}
}
